package com.iris.xml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomDemo {
	private Document document;
	
	public DomDemo(){
		init();
	}

	public void init() {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			this.document = builder.newDocument();
		} catch (ParserConfigurationException e) {
			System.out.println(e.getMessage());
		}
	}
	
	public void createFileName(String fileName){
		try {
			InputStream is = new FileInputStream(fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void createXml(String fileName) {
		Element root = this.document.createElement("employees");
		this.document.appendChild(root);
		Element employee = this.document.createElement("employee");
		Element name = this.document.createElement("name");
		name.appendChild(this.document.createTextNode("丁宏亮"));
		employee.appendChild(name);
		Element sex = this.document.createElement("sex");
		sex.appendChild(this.document.createTextNode("m"));
		employee.appendChild(sex);
		Element age = this.document.createElement("age");
		age.appendChild(this.document.createTextNode("30"));
		employee.appendChild(age);
		root.appendChild(employee);
		TransformerFactory tf = TransformerFactory.newInstance();
		try {
			Transformer transformer = tf.newTransformer();
			DOMSource source = new DOMSource(document);
			transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
			StreamResult result = new StreamResult(pw);
			transformer.transform(source, result);
			System.out.println("生成XML文件成功!");
		} catch (TransformerConfigurationException e) {
			System.out.println(e.getMessage());
		} catch (IllegalArgumentException e) {
			System.out.println(e.getMessage());
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (TransformerException e) {
			System.out.println(e.getMessage());
		}
	}

	public void parserXml(File file) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
//			InputStream input = 
//			ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes("GBK"));
			InputStream input = new FileInputStream(file);
			Document document = db.parse(input);
			NodeList systems = document.getChildNodes();
			System.out.println("systems==="+systems.getLength());
			System.out.println("systems.getLength()==="+systems.getLength());
			String idTmp="";
			for (int i = 0; i < systems.getLength(); i++) {
				Node system = systems.item(i);
				NodeList systemInfo = system.getChildNodes();
				for (int j = 0; j < systemInfo.getLength(); j++) {
					Node node = systemInfo.item(j);
//					System.out.println(node.getLocalName()+"=="+node.getNodeName());
					if(node.getNodeName().equals("System")){
						NamedNodeMap attrs = node.getAttributes();
						if(attrs!=null){
							TInterProductInfo systemxml = new TInterProductInfo();
							Node attrNode = attrs.getNamedItem("AppType");
							if(attrNode!=null){
								systemxml.setAppType(attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("AssistManagers");
							if(attrNode!=null){
								systemxml.setAssistManagers( attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("Description");
							if(attrNode!=null){
								systemxml.setDescription( attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("DevelopType");
							if(attrNode!=null){
								systemxml.setDevelopType( attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("Id");
							if(attrNode!=null){
								idTmp =  attrNode.getNodeValue();
								systemxml.setProductId( attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("ImportantLevel");
							if(attrNode!=null){
								systemxml.setImportantLevel( attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("MajarManager");
							if(attrNode!=null){
								systemxml.setMajarManager( attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("MajarTeam");
							if(attrNode!=null){
								systemxml.setMajarTeam( attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("Name");
							if(attrNode!=null){
								System.out.println("update t_inter_product_info set PRODUCT_NAME='"+attrNode.getNodeValue()+"' where PRODUCT_ID='"+idTmp+"';");
								System.out.println("update t_md_inst  set inst_name='"+attrNode.getNodeValue()+"' where class_id='DDSystem' and  inst_code='"+idTmp+"';");
								systemxml.setProductName( attrNode.getNodeValue());
							}
							attrNode = attrs.getNamedItem("Status");
							if(attrNode!=null){
								systemxml.setStatus( attrNode.getNodeValue());
							}
//							System.out.println("attrs.getLength()=="+attrs.getLength());
//							for (int k = 0; k < attrs.getLength(); k++) {
//								Node attrNode = attrs.item(k);
//								System.out.print(attrNode.getNodeName()+":"+attrNode.getNodeValue()+",");
//							}
//							System.out.println();
						}
					}
//					NodeList employeeMeta = node.getChildNodes();
//					for (int k = 0; k < employeeMeta.getLength(); k++) {
//						System.out.println(
//								employeeMeta.item(k).getNodeName() + ":" + employeeMeta.item(k).getTextContent());
//					}
				}
			}
			System.out.println("解析完毕");
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (ParserConfigurationException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}
	public void test(){
		System.out.println(this.getClass().getResource("").getPath());
	}
	
	public static void main(String[] args) {
		DomDemo dom = new DomDemo();
		//xmlsystems3.xml
		//xmlsystems2.xml_error
//		File file = new File("D:\\new_code\\thinking\\src\\com\\iris\\xml\\xmlsystems3.xml");
		File file = new File("D:\\new_code\\thinking\\src\\com\\iris\\xml\\xmlsystems2.xml_error");
		dom.parserXml(file);
	}
}